//
// AMX Mod X 1.76 Script
//
// Developed by The AMX Mod X DoD Community
// http://www.dodplugins.net
//
// Author: [RST] FireStorm
// Updates: diamond-optic
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//
//
// In-Game: 
// ======== 
// 
// amx_ammogiver <1/0>       =  enable/disable DoD AmmoGiver 
//                               (admin command) 
// 
// say /giveammo             =  get specified amount of ammo 
//                               for weapon in hand
// 
// say giveammo              =  get specified amount of ammo 
//                               for weapon in hand 
//
//
// CVARs (for amxx.cfg):
// =====================
//
// dod_ammogiver 1            //enable DoD AmmoGiver
//                               by default
//
// dod_ammogiver_maxrefills   //set how many times ammo can be
//                            //-refilled between respawns
// dod_ammogiver_bullets      //set how many bullets to give
//                            //-(pistols/rifles/smgs/mgs)
// dod_ammogiver_grenades     //set how many grenades to give
//                            //-(no need to explain)
// dod_ammogiver_shells       //set how many shells to give
//                            //-(bazooka/panzerschreck/piat)
//
//
// This plugin allows players to refill the ammo of the weapon/grenade
// currently in their hand by saying "/giveammo".
// You can set how often they can refill their ammo between respawns,
// as well as the amount of bullets, shells and grenades to give.
//
//
// AMX Mod X 1.76, DoDx & DoDFun modules required!
//
//
// To do:
//
// - nothing so far :P
//
//
// Changelog:
//
// - 25.10.2004 Version 0.8beta
//	Initial Beta
//
// - 19.12.2006 Version 1.0
//	No need to be a beta anymore!
//	Added public cvar for tracking
//	Used dod_client_spawn instead of ResetHud
//	Changed client_authorized to client_putinserver
//	Replaced some IF statements with switches
//	Added PCVARS
//	Added some 'connected' & 'alive' checks
//	Got rid of unneccesary code
//	Put 'dod' prefix on cvars (didnt change console command)
//	Made code more 'readable'
//	
//


#include <amxmodx>
#include <amxmisc>
#include <dodx>
#include <dodfun>

new gotAmmo[33]
new p_ammogiver, p_shells, p_grenades, p_bullets, p_maxrefills

public plugin_init()
{
	register_plugin("DoD AmmoGiver","1.0","AMXX DoD Team")
	register_cvar("dod_ammogiver_stats","1.0",FCVAR_SERVER|FCVAR_SPONLY)
   
	p_ammogiver = register_cvar("dod_ammogiver","1")

	p_shells = register_cvar("dod_ammogiver_shells","15")
	p_grenades = register_cvar("dod_ammogiver_grenades","5")
	p_bullets = register_cvar("dod_ammogiver_bullets","500")
	p_maxrefills = register_cvar("dod_ammogiver_maxrefills","1")
   
	register_concmd("amx_ammogiver","set_ammogiver",ADMIN_BAN,"<enable/disable DoD AmmoGiver>")
   
	register_clcmd("say /giveammo","gimme_ammo",-1)
	register_clcmd("say giveammo","gimme_ammo",-1)
	register_clcmd("say_team /giveammo","gimme_ammo",-1)
	register_clcmd("say_team giveammo","gimme_ammo",-1)
}

public client_putinserver(id)
{
	if(get_pcvar_num(p_ammogiver) && is_user_connected(id))
		gotAmmo[id] = 0
}

public dod_client_spawn(id)
{
	if(get_pcvar_num(p_ammogiver) && is_user_connected(id))
		gotAmmo[id] = 0
}

public set_ammogiver(id,level,cid)
{
	if(!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED
		
	new ammogiver_s[2]
	read_argv(1,ammogiver_s,2)

	switch(str_to_num(ammogiver_s))
		{
		case 0: {
			switch(get_pcvar_num(p_ammogiver))
				{
				case 0: client_print(id,print_chat,"[AMXX] DoD AmmoGiver is already disabled.....")
				case 1: {
					set_pcvar_num(p_ammogiver,0)
					client_print(id,print_chat,"[AMXX] DoD AmmoGiver DEACTIVATED.....")
					}
				}
			}
		case 1: {
			switch(get_pcvar_num(p_ammogiver))
				{
				case 0: {
					set_pcvar_num(p_ammogiver,1)
					client_print(id,print_chat,"[AMXX] DoD AmmoGiver ACTIVATED.....")
					}
				case 1: client_print(id,print_chat,"[AMXX] DoD AmmoGiver is already enabled.....")
				}
			}
		}
	
	return PLUGIN_HANDLED
}

public gimme_ammo(id)
{
	if(is_user_connected(id) && is_user_alive(id))
		{ 
		new zMaxRefill = get_pcvar_num(p_maxrefills)
			
		if(!get_pcvar_num(p_ammogiver))
			client_print(id,print_chat,"[AMXX] DoD AmmoGiver is disabled, ask an admin to enable it!")
			
		else if(gotAmmo[id] < zMaxRefill)
			{
			new playername[32]
			get_user_name(id,playername,31)
			
			new ammo, clip, zGun = dod_get_user_weapon(id,clip,ammo)
		       
			if(zGun == DODW_AMERKNIFE || zGun == DODW_BRITKNIFE || zGun == DODW_SPADE || zGun == DODW_GERKNIFE)
				client_print(id,print_chat,"[DoD AmmoGiver] I don't think that knives/spades can shoot, do you?!")
	
			else if(zGun==DODW_BAZOOKA || zGun==DODW_PANZERSCHRECK || zGun==DODW_PIAT)
				{
				new zShells = get_pcvar_num(p_shells)
				dod_set_user_ammo(id,zGun,zShells)
				
				client_print(id,print_chat,"[DoD AmmoGiver] %s gave himself %d shells for his Bazooka/Panzerschreck/Piat!",playername,zShells)
				
				gotAmmo[id] ++
				}
	       
			else if(zGun==DODW_HANDGRENADE || zGun==DODW_STICKGRENADE || zGun==DODW_MILLS_BOMB)
				{
				new zGrenades = get_pcvar_num(p_grenades)
				dod_set_user_ammo(id,zGun,zGrenades)
				
				client_print(id,print_chat,"[DoD AmmoGiver] %s gave himself %d grenades!",playername,zGrenades)
				
				gotAmmo[id] ++
				}
	       
			else
				{
				new zBullets = get_pcvar_num(p_bullets)
				dod_set_user_ammo(id,zGun,zBullets)
				
				if(zGun == DODW_LUGER || zGun == DODW_COLT || zGun == DODW_WEBLEY)
					client_print(id,print_chat,"[DoD AmmoGiver] %s gave himself %d bullets for his pistol!",playername,zBullets)
				else
					client_print(id,print_chat,"[DoD AmmoGiver] %s gave himself %d bullets for his gun!",playername,zBullets)
				
				gotAmmo[id] ++
				}	       
			}
   
		else if(gotAmmo[id] >= zMaxRefill)
			client_print(id,print_chat,"[DoD AmmoGiver] You already filled your ammo %d times! Wait for next respawn!",gotAmmo[id])
			
		}
		
	return PLUGIN_HANDLED   
}
